<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ComplexNumber 类 | Math Complex</title>
    <meta name="description" content="简单易用的JavaScript复数扩展数学库">

    
    <link rel="preload" href="/math-complex/assets/css/0.styles.dc9a62cc.css" as="style"><link rel="preload" href="/math-complex/assets/js/app.a7e80bc5.js" as="script"><link rel="preload" href="/math-complex/assets/js/2.b96ecc85.js" as="script"><link rel="preload" href="/math-complex/assets/js/6.0954e40b.js" as="script"><link rel="prefetch" href="/math-complex/assets/js/10.e877db03.js"><link rel="prefetch" href="/math-complex/assets/js/11.aa6e74d4.js"><link rel="prefetch" href="/math-complex/assets/js/12.ef27bf30.js"><link rel="prefetch" href="/math-complex/assets/js/13.6d5e4273.js"><link rel="prefetch" href="/math-complex/assets/js/3.3a2d5ce2.js"><link rel="prefetch" href="/math-complex/assets/js/4.5f9aa696.js"><link rel="prefetch" href="/math-complex/assets/js/5.8bdadb5e.js"><link rel="prefetch" href="/math-complex/assets/js/7.30d57aa9.js"><link rel="prefetch" href="/math-complex/assets/js/8.6149196e.js"><link rel="prefetch" href="/math-complex/assets/js/9.6ecdb287.js">
    <link rel="stylesheet" href="/math-complex/assets/css/0.styles.dc9a62cc.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/math-complex/" class="home-link router-link-active"><!----> <span class="site-name">Math Complex</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/math-complex/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/math-complex/guide/" class="nav-link">
  入门指南
</a></div><div class="nav-item"><a href="/math-complex/API/" class="nav-link router-link-active">
  API
</a></div><div class="nav-item"><a href="/math-complex/appendix/" class="nav-link">
  附录
</a></div><div class="nav-item"><a href="https://github.com/shawroger/math-complex" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/math-complex/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/math-complex/guide/" class="nav-link">
  入门指南
</a></div><div class="nav-item"><a href="/math-complex/API/" class="nav-link router-link-active">
  API
</a></div><div class="nav-item"><a href="/math-complex/appendix/" class="nav-link">
  附录
</a></div><div class="nav-item"><a href="https://github.com/shawroger/math-complex" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>API</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/math-complex/API/" class="sidebar-link">全局对象</a></li><li><a href="/math-complex/API/complex-number.html" class="active sidebar-link">ComplexNumber 类</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#对象属性" class="sidebar-link">对象属性</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#imaginary" class="sidebar-link">imaginary</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#real" class="sidebar-link">real</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#radius" class="sidebar-link">radius</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#mainangle" class="sidebar-link">mainAngle</a></li></ul></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#静态方法" class="sidebar-link">静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#define" class="sidebar-link">define</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#fromangle" class="sidebar-link">fromAngle</a></li></ul></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#对象方法" class="sidebar-link">对象方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#abs" class="sidebar-link">abs</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#abssquare" class="sidebar-link">absSquare</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#add" class="sidebar-link">add</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#angle" class="sidebar-link">angle</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#contrary" class="sidebar-link">contrary</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#conj" class="sidebar-link">conj</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#divide" class="sidebar-link">divide</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#isangle" class="sidebar-link">isAngle</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#mult" class="sidebar-link">mult</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#pow" class="sidebar-link">pow</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#recip" class="sidebar-link">recip</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#sub" class="sidebar-link">sub</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#subtract" class="sidebar-link">subtract</a></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#sqrt" class="sidebar-link">sqrt</a></li></ul></li><li class="sidebar-sub-header"><a href="/math-complex/API/complex-number.html#关于链式操作" class="sidebar-link">关于链式操作</a></li></ul></li><li><a href="/math-complex/API/complex-reflect.html" class="sidebar-link">ComplexReflect 静态类</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="complexnumber-类"><a href="#complexnumber-类" class="header-anchor">#</a> ComplexNumber 类</h1> <p><strong>ComplexNumber</strong> 是本库的核心类，所有其他类都由此衍生，且通过 <strong>new</strong> 即可声明一个复数对象。</p> <ul><li><strong>constructor</strong> 类型定义</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token function">constructor</span><span class="token punctuation">(</span>real<span class="token operator">?</span><span class="token operator">:</span> number<span class="token punctuation">,</span> imaginary<span class="token operator">?</span><span class="token operator">:</span> number<span class="token punctuation">)</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
</code></pre></div><h2 id="对象属性"><a href="#对象属性" class="header-anchor">#</a> 对象属性</h2> <h3 id="imaginary"><a href="#imaginary" class="header-anchor">#</a> imaginary</h3> <p><strong>imaginary</strong> 是一个 <strong>number</strong> 对象，即为该对象的虚部。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span>imaginary <span class="token operator">===</span> <span class="token number">4</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="real"><a href="#real" class="header-anchor">#</a> real</h3> <p><strong>real</strong> 是一个 <strong>number</strong> 对象，即为该对象的实部。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span>real <span class="token operator">===</span> <span class="token number">3</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="radius"><a href="#radius" class="header-anchor">#</a> radius</h3> <p><strong>radius</strong> 是一个 <strong>number</strong> 对象，即为该对象的模长。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span>radius <span class="token operator">===</span> <span class="token number">5</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="mainangle"><a href="#mainangle" class="header-anchor">#</a> mainAngle</h3> <p><strong>mainAngle</strong> 是一个 <strong>number</strong> 对象，即为该对象的幅角主值。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 1 + i</span>
z1<span class="token punctuation">.</span>mainAngle <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">4</span> <span class="token comment">// true</span>
</code></pre></div><h2 id="静态方法"><a href="#静态方法" class="header-anchor">#</a> 静态方法</h2> <h3 id="define"><a href="#define" class="header-anchor">#</a> define</h3> <p><strong>define</strong> 是一个 <strong>ComplexNumber</strong> 对象的构造器，返回一个ComplexNumber对象。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">static</span> <span class="token function">define</span><span class="token punctuation">(</span>real<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> imaginary<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token operator">=</span> ComplexNumber<span class="token punctuation">;</span>
<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
</code></pre></div><p>本质上 <strong>define</strong> 就是调用了<strong>ComplexNumber</strong> 类的 <strong>new</strong> 方法：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>ComplexNumber<span class="token punctuation">.</span><span class="token function-variable function">define</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)</span>
</code></pre></div><h3 id="fromangle"><a href="#fromangle" class="header-anchor">#</a> fromAngle</h3> <p><strong>fromAngle</strong> 是另一个 <strong>ComplexNumber</strong> 对象的构造器，返回一个ComplexNumber对象，与 <strong>define</strong> 不同，<strong>fromAngle</strong> 接收的参数是复数的模长和幅角主值。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">static</span> <span class="token function">fromAngle</span><span class="token punctuation">(</span>radius<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> mainAngle<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ComplexNumber <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> fromAngle <span class="token punctuation">}</span> <span class="token operator">=</span> ComplexNumber<span class="token punctuation">;</span>
<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">fromAngle</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// z1 = sqrt(2) * (cos(π/4) + sin(π/4)) = 1 + i</span>
</code></pre></div><h2 id="对象方法"><a href="#对象方法" class="header-anchor">#</a> 对象方法</h2> <h3 id="abs"><a href="#abs" class="header-anchor">#</a> abs</h3> <p><strong>abs</strong> 返回一个 <strong>ComplexNumber</strong> 对象的模长。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 此处 define 即为 ComplexNumber.define 在全局的导出对象</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">5</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="abssquare"><a href="#abssquare" class="header-anchor">#</a> absSquare</h3> <p><strong>absSquare</strong> 返回一个 <strong>ComplexNumber</strong> 对象模长的平方。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">absSquare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span><span class="token function">absSquare</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">25</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="add"><a href="#add" class="header-anchor">#</a> add</h3> <p><strong>add</strong> 定义了 <strong>ComplexNumber</strong> 的加法运算，且支持原生<strong>number</strong> 对象。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">add</span><span class="token punctuation">(</span>addend<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">|</span> ComplexNumber<span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
<span class="token keyword">const</span> z2 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 5 + 12i</span>
<span class="token keyword">const</span> z3 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z3 = z1 + 2 = 5 + 4i</span>
<span class="token keyword">const</span> z4 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>z2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z4 = z1 + z2 = 8 + 16i</span>
</code></pre></div><h3 id="angle"><a href="#angle" class="header-anchor">#</a> angle</h3> <p><strong>angle</strong> 返回 <strong>ComplexNumber</strong> 的辐角主值。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">angle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
z1<span class="token punctuation">.</span><span class="token function">angle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">4</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="contrary"><a href="#contrary" class="header-anchor">#</a> contrary</h3> <p><strong>contrary</strong> 返回一个 <strong>ComplexNumber</strong> 对象的相反数。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">contrary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span><span class="token function">contrary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>real <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">3</span> <span class="token comment">// true</span>
z1<span class="token punctuation">.</span><span class="token function">contrary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>imaginary <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">4</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="conj"><a href="#conj" class="header-anchor">#</a> conj</h3> <p><strong>conj</strong> 返回一个 <strong>ComplexNumber</strong> 对象的共轭复数。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">conj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
z1<span class="token punctuation">.</span><span class="token function">conj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>real <span class="token operator">===</span> <span class="token number">3</span> <span class="token comment">// true</span>
z1<span class="token punctuation">.</span><span class="token function">conj</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>imaginary <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">4</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="divide"><a href="#divide" class="header-anchor">#</a> divide</h3> <p><strong>divide</strong> 定义了 <strong>ComplexNumber</strong> 的除法运算，且支持原生<strong>number</strong> 对象。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">divide</span><span class="token punctuation">(</span>divisor<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">|</span> ComplexNumber<span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define<span class="token punctuation">,</span> tryRealize <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 6 + 8i</span>
<span class="token keyword">const</span> z2 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">divide</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// z2 = z1 / 2 = 3 + 4i</span>
<span class="token function">tryRealize</span><span class="token punctuation">(</span>z1<span class="token punctuation">.</span><span class="token function">divide</span><span class="token punctuation">(</span>z2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">2</span> <span class="token comment">// true, z1 / z2 = 2</span>
</code></pre></div><h3 id="isangle"><a href="#isangle" class="header-anchor">#</a> isAngle</h3> <p><strong>isAngle</strong> 用来检查一个弧度角是否是 <strong>ComplexNumber</strong> 的一个辐角（即与辐角主值相差2π的整数倍）。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">isAngle</span><span class="token punctuation">(</span>angle<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> accuracy<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> angle <span class="token operator">=</span> <span class="token number">9</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">4</span><span class="token punctuation">;</span>
z1<span class="token punctuation">.</span><span class="token function">isAngle</span><span class="token punctuation">(</span>angle<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token boolean">true</span> <span class="token comment">// true</span>
</code></pre></div><ul><li>设置精度accuracy</li></ul> <p>有时由于浮点计算问题，对π的检查存在偏差，我们可以适当设置精度 <strong>accuracy</strong> 参数的值，默认为0.0001，<strong>accuracy</strong> 的定义是 (角度差/2π) 的小数部分。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

z1<span class="token punctuation">.</span><span class="token function">isAngle</span><span class="token punctuation">(</span>angle<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token boolean">false</span> <span class="token comment">// false，应为true，精度过小</span>
z1<span class="token punctuation">.</span><span class="token function">isAngle</span><span class="token punctuation">(</span>angle<span class="token punctuation">,</span> <span class="token number">0.01</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token boolean">true</span> <span class="token comment">// 精度合理</span>
</code></pre></div><h3 id="mult"><a href="#mult" class="header-anchor">#</a> mult</h3> <p><strong>mult</strong> 定义了 <strong>ComplexNumber</strong> 的乘法运算，且支持原生<strong>number</strong> 对象。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">mult</span><span class="token punctuation">(</span>multiplier<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">|</span> ComplexNumber<span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
<span class="token keyword">const</span> z2 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">mult</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// z2 = z1 * 2 = 6 + 8i</span>
</code></pre></div><h3 id="pow"><a href="#pow" class="header-anchor">#</a> pow</h3> <p><strong>pow</strong> 定义了 <strong>ComplexNumber</strong> 的乘方运算。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">pow</span><span class="token punctuation">(</span>power<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber <span class="token operator">|</span> <span class="token number">1</span><span class="token punctuation">;</span>
</code></pre></div><p>幂必须是整数，如果幂为分数参见 <a href="/math-complex/API/complex-number.html#sqrt">sqrt 方法</a>，且乘方运算在幂不同时候执行不同的运算方法。</p> <ul><li><p>幂为 0 时，返回值为 1</p></li> <li><p>幂为正整数时，返回值为复数自乘 n 次的结果</p></li> <li><p>幂为负整数时，返回值为复数自乘 n 次的结果的倒数 （参见 <a href="/math-complex/API/complex-number.html#recip">recip 方法</a>）</p></li></ul> <h3 id="recip"><a href="#recip" class="header-anchor">#</a> recip</h3> <p><strong>recip</strong> 返回一个 <strong>ComplexNumber</strong> 的倒数结果。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">recip</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
<span class="token keyword">const</span> z2 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">recip</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// z2 = 1 / z1 = 0.6- 0.8i</span>
</code></pre></div><ul><li>实际上，<strong>recip()</strong> 方法就是返回 <strong>1 + 0i</strong> 的<a href="/math-complex/API/complex-number.html#divide">除法运算</a>结果。</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">ComplexNumber</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    <span class="token function">recip</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ComplexNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">divide</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="sub"><a href="#sub" class="header-anchor">#</a> sub</h3> <p><strong>substract</strong> 的别名，参见 <a href="/math-complex/API/complex-number.html#subtract">subtract</a>。</p> <h3 id="subtract"><a href="#subtract" class="header-anchor">#</a> subtract</h3> <p><strong>subtract</strong> 定义了 <strong>ComplexNumber</strong> 对象的减法运算。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">subtract</span><span class="token punctuation">(</span>subtrahend<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">|</span> ComplexNumber<span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> define <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;math-complex&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> z1 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 3 + 4i</span>
<span class="token keyword">const</span> z2 <span class="token operator">=</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z1 = 5 + 12i</span>
<span class="token keyword">const</span> z3 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z3 = z1 - 2 = 1 + 4i</span>
<span class="token keyword">const</span> z4 <span class="token operator">=</span> z1<span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span>z2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// z4 = z1 - z2 = -2 + -7i</span>
</code></pre></div><ul><li>实际上，<strong>subtract()</strong> 方法就是参数的相反数的<a href="/math-complex/API/complex-number.html#add">加法运算</a>结果。</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">ComplexNumber</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    <span class="token function">subtract</span><span class="token punctuation">(</span><span class="token parameter">params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">contrary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="sqrt"><a href="#sqrt" class="header-anchor">#</a> sqrt</h3> <p><strong>sqrt</strong> 定义了 <strong>ComplexNumber</strong> 对象的开方运算。</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">sqrt</span><span class="token punctuation">(</span>power<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> ComplexNumber<span class="token punctuation">;</span>
</code></pre></div><ul><li><p><strong>power</strong> 必须为正整数，默认空参数是开平方，即 <strong>power</strong> 为2。</p></li> <li><p>开方运算是利用棣莫佛定理，将复数转换成模长和辐角的形式计算，参见 <a href="/math-complex/API/complex-number.html#fromangle">fromAngle</a> 静态方法。</p></li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">ComplexNumber</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    <span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token parameter">power</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> ComplexNumber<span class="token punctuation">.</span><span class="token function">fromAngle</span><span class="token punctuation">(</span>
			Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>radius<span class="token punctuation">,</span> <span class="token number">1</span> <span class="token operator">/</span> power<span class="token punctuation">)</span><span class="token punctuation">,</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>mainAngle <span class="token operator">/</span> power
		<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="关于链式操作"><a href="#关于链式操作" class="header-anchor">#</a> 关于链式操作</h2> <p>所有返回 <strong>ComplexNumber</strong> 对象的方法都支持链式运算。</p> <p>其中包含：<a href="/math-complex/API/complex-number.html#add">add</a>, <a href="/math-complex/API/complex-number.html#contrary">contrary</a>, <a href="/math-complex/API/complex-number.html#conj">conj</a>, <a href="/math-complex/API/complex-number.html#divide">divide</a>, <a href="/math-complex/API/complex-number.html#mult">mult</a>, <a href="/math-complex/API/complex-number.html#recip">recip</a>, <a href="/math-complex/API/complex-number.html#subtract">subtract</a>, <a href="/math-complex/API/complex-number.html#sqrt">sqrt</a></p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/math-complex/API/" class="prev router-link-active">
        全局对象
      </a></span> <span class="next"><a href="/math-complex/API/complex-reflect.html">
        ComplexReflect 静态类
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/math-complex/assets/js/app.a7e80bc5.js" defer></script><script src="/math-complex/assets/js/2.b96ecc85.js" defer></script><script src="/math-complex/assets/js/6.0954e40b.js" defer></script>
  </body>
</html>
